<?php

class CooperationController extends Core_Controller_Action 
{	
	public $menuType = 5;
	public $_news;
	public $newsLimit = 12;
	
	public function init() {
		$this->view->menuType = $this->menuType;
		$this->_news = new News();
	}
	
	public function indexAction() {

		
		$item = $this->_news->getListService(5,1,true)->getFirst();
		
		if($item)
		{
		$this->view->list = $item;	
		}
		else
		{
			$this->view->error = true;	
		}
			
	}
	
	public function showAction() {
	
	$id = intval( $this->_request->getParam( 'id', 0 ) );
		$page = intval( $this->_request->getParam( 'page', 1 ) );
		$parent = intval( $this->_request->getParam( 'p', 0 ) );
		
		$pager = null;
		$tmp = null;
		
		if( $id > 0 ) {			
			$this->view->id = $id;
			$this->view->parent = $parent;
	
			$cat_type = new Categries();
			$type = $cat_type->getItem($id)->getFirst();
			
			$pager = null;
			
			if($type['type'] == 1)
			{
				$pager = $this->_news->getListByCategoryWithPagerService( $id, $this->view->baseUrl(). '/cooperation/show/page/{%page_number}/id/'.$id.'/p/'.$parent, $page, $this->newsLimit );
				$this->view->type = $type['type'];
			}	
			
			else
			{	
			$pager = $this->_news->getListByCategoryWithPagerService( $id, "", 1, 1);
			$this->view->type = 2;
			
			if($pager)
			{
			$tmp = $pager['layout']->execute()->getFirst();
			//$this->_redirect( 'cooperation/article/id/'.$tmp['news_id'] ); 
			}
			
			
			}
			
		if( $pager ) {
				if($type['type'] == 1)
				{
				$pagerLayout = $pager['layout'];
				$pagerLayout->setTemplate('<a class="nums" href="{%url}">{%page}</a>');
				$pagerLayout->setSelectedTemplate('<a class="nums focus" href="{%url}">{%page}</a>');
				
				$pager = $pager['pager'];
		
				$this->view->list = $pagerLayout->execute();
				$this->view->pager = $pagerLayout;
				
				$this->view->result = $pager->getNumResults();
				$this->view->page = $page;
				$this->view->pathInfo = $this->view->baseUrl() . $this->_request->getPathInfo();
				$this->view->limit = $this->newsLimit;
				}
				else
				{
				
				try{
				$list = $this->_news->getItemService( $tmp['news_id'] );	
				$this->view->list = $list->getFirst()->toArray();
				}
				catch(Exception $e){	
				
				$this->view->type = 1;
				$this->view->emptyResult = true;
				}
				}
			
            	$flash = $this->_helper->FlashMessenger->getMessages();
            	
            	if( (isset($flash[0])) && $flash[0]=='emailsent')
            	{
            	    $this->view->sendInfo = true;
            	}
            		
            	if( $this->_request->isPost() ) 
            	{
            		
        			$post = $this->_request->getPost();
        
        			$b  = '<b>Imie i Nazwisko</b>: ' . $post['namesurname'] . '<br />';
        			$b .= '<b>Nazwa firmy</b>: ' . $post['companyname'] . '<br />';
        			$b .= '<b>Email</b>: ' . $post['email'] . '<br />';
        			$b .= '<b>Telefon</b>: ' . $post['phone'] . '<br />';
        			$b .= '<b>Zapytanie</b>: ' . $post['inquiry'] . '<br />';
        			
        			$mail = new Core_Mail();
        			$mail->addTo( 'a3j6exm@gmail.com', 'Carnauba' );
        			$mail->setFrom( $post['email'], $post['email'] );
        			$mail->setSubject( 'Zgłoszenie do kluby Carnauba' );
        			$mail->setBodyText( strip_tags( $b ) );
        			$mail->setBodyHtml( $b );
        			$mail->send();
        			
                    $this->_helper->FlashMessenger('emailsent');
        			$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        			$this->_redirect($url, array('code' => 303));
            			
        		}
		
		
		} else {
				$this->view->emptyResult = true;
			}
		} else {
			$this->_forward( 'p404', 'index' );
		}
	
	}
	
	
	public function articleAction() {
		$id = intval( $this->_request->getParam( 'id', 0 ) );

		if( $id ) {
			$item = $this->_news->getItemService( $id );
			if( $item ) {				
				$this->view->list = $item->getFirst()->toArray();
			} else {
				$this->_forward( 'p404', 'index' );
			}
		} else {
			 $this->view->emptyResult = true;
			//$this->_forward( 'p404', 'index' );
		}
		
	$flash = $this->_helper->FlashMessenger->getMessages();
	
	if( (isset($flash[0])) && $flash[0]=='emailsent')
	{
	    $this->view->sendInfo = true;
	}
		
	if( $this->_request->isPost() ) {
		
			$post = $this->_request->getPost();

			$b  = '<b>Imie i Nazwisko</b>: ' . $post['namesurname'] . '<br />';
			$b .= '<b>Nazwa firmy</b>: ' . $post['companyname'] . '<br />';
			$b .= '<b>Email</b>: ' . $post['email'] . '<br />';
			$b .= '<b>Telefon</b>: ' . $post['phone'] . '<br />';
			$b .= '<b>Zapytanie</b>: ' . $post['inquiry'] . '<br />';
			
			$mail = new Core_Mail();
			$mail->addTo( 'a3j6exm@gmail.com', 'Carnauba' );
			$mail->setFrom( $post['email'], $post['email'] );
			$mail->setSubject( 'Zgłoszenie do kluby Carnauba' );
			$mail->setBodyText( strip_tags( $b ) );
			$mail->setBodyHtml( $b );
			$mail->send();
			
            $this->_helper->FlashMessenger('emailsent');
			$url = 'http://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
			$this->_redirect($url, array('code' => 303));
			
		}
	}

}